*************************************************************************************
* Figure A.3: excluding European election and subnational time window
use  "Data_temp/turnout.dta", clear

tab election, gen(elec_dummy)
gen rel_age=.

* drop those in +/- 6 months vicinity of EP for that sample
gen ep1979=ym(1979,6)
gen ep1984=ym(1984,6)
gen ep1989=ym(1989,6)
gen ep1994=ym(1994,6)
gen ep1999=ym(1999,6)
gen ep2004=ym(2004,6)
gen ep2009=ym(2009,6)
gen ep2014=ym(2014,6)

format ep* %tm

foreach x in 1979 1984 1989 1994 1999 2004 2009 2014 {
	gen age_ep`x'=(ep`x'-birth_date)-216 // 12*18=216
	}
global ep_drop	"!inrange(age_ep1979,-6,5) & !inrange(age_ep1984,-6,5) & !inrange(age_ep1989,-6,5) & !inrange(age_ep1994,-6,5) & !inrange(age_ep1999,-6,5) & !inrange(age_ep2004,-6,5) & !inrange(age_ep2009,-6,5) & !inrange(age_ep2014,-6,5)" 


global elec0		elec_dummy1-elec_dummy6
global elec1		elec_dummy1-elec_dummy6
global elec2		elec_dummy1-elec_dummy6
global elec3		elec_dummy1-elec_dummy6
global elec4		elec_dummy2-elec_dummy6
global elec5		elec_dummy1 elec_dummy3-elec_dummy6
global elec6		elec_dummy1-elec_dummy2 elec_dummy4-elec_dummy6

forvalues c = 0/3 {

	* Main
	rdrobust voted age`c' if age`c'!=0			 , covs(${elec`c'}) p(1)  h(24)
		regsave RD_Estimate using "Data_temp/election`c'_p1.dta", replace ci addlabel(elec, `c', spec, "ref")

	* Excluding EP window	
	rdrobust voted age`c' if age`c'!=0 & ${ep_drop}, covs(${elec`c'}) p(1)  h(24)
		regsave RD_Estimate using "Data_temp/election`c'_p2.dta", replace ci addlabel(elec, `c', spec, "euro")
		
	* England only 	
	rdrobust voted age`c' if age`c'!=0 &region<17, covs(${elec`c'}) p(1)  h(24)
		regsave RD_Estimate using "Data_temp/election`c'_p3.dta", replace ci addlabel(elec, `c', spec, "england")	
		
		}

**************************************************************
* pooled 

* duplicate observations going into either estimate
gen m1=1 if inrange(age1,-48,47) & inrange(age2,-24,24)		
tab m1
expand 2 if m1==1, gen(dupl12)
replace age2=. if dupl12==1 // ignore those from duplicate observations
replace age1=. if dupl12==0 & inrange(age2,-24,24) //
count if inrange(age1,-48,47) & inrange(age2,-24,24)

gen m2=1 if inrange(age2,-24,24) & inrange(age3,-24,24)		
expand 2 if m2==1, gen(dupl23)
replace age2=. if dupl23==1
replace age3=. if dupl23==0 & inrange(age2,-24,24) 
count if inrange(age2,-24,24) & inrange(age3,-24,24)

forvalues x = 1/3 {
		replace rel_age=age`x' if inrange(age`x',-24,24) & rel_age==.
		}

rdrobust voted rel_age if rel_age!=0, 				covs(elec_dummy1-elec_dummy6) p(1)  h(24)
	regsave RD_Estimate using "Data_temp/election_pooled_1.dta", replace ci addlabel(elec, 4, spec, "ref")
rdrobust voted rel_age if rel_age!=0 & ${ep_drop}, 	covs(elec_dummy1-elec_dummy6) p(1)  h(24) 
	regsave RD_Estimate using "Data_temp/election_pooled_2.dta", replace ci addlabel(elec, 4, spec, "euro")
rdrobust voted rel_age if rel_age!=0 & region<17, 	covs(elec_dummy1-elec_dummy6) p(1)  h(24)
	regsave RD_Estimate using "Data_temp/election_pooled_3.dta", replace ci addlabel(elec, 4, spec, "england")
	
	
	
clear	
forvalues c=0/3 {
	forvalues p = 1/3 {
		append using "Data_temp/election`c'_p`p'.dta"
		}
	cap append using "Data_temp/election_pooled_`c'.dta"	
	}
	
	
gen n=1 if spec=="ref"	
replace n=2 if spec=="euro"
replace n=3 if spec=="england"
	
sort elec n

replace elec=elec-0.1 if n==1
replace elec=elec+0.1 if n==3
	
	
twoway (rcap ci_lo ci_up elec, lcolor(edkblue*0.8)) ///
(scatter coef elec if n==1,  msize(medlarge) msymbol(D) mfcolor(white) mlwidth(medthick) mlcolor(edkblue)) ///
(scatter coef elec if n==2, msize(medlarge) msymbol(O) mfcolor(white) mlwidth(medthick) mlcolor(edkblue)) ///
(scatter coef elec if n==3, msize(medlarge) msymbol(T) mfcolor(white) mlwidth(medthick) mlcolor(edkblue)) , ///
yl(-.1(0.1)0.6) xsc(r(-0.5 4.5))  xt("") ///
xlabel(none) legend(order(2 "Main estimates" 3 "Excluding EP window" 4 "England only" ) row(1)  subt("Estimation sample")) ///
subt("")  yt("RD estimate") ///
 xlab(0 "t{subscript:0}" 1 "t{subscript:1}" 2 "t{subscript:2}" 3 "t{subscript:3}" 4 `""t{subscript:1}-t{subscript:3}" "pooled""') xline(3.5, lwidth(thin)) yline(0) ///
 graphregion(margin(zero)) 		yline(0, lwidth(thin) lcolor(gs12) lpattern(solid))

graph export "Figures/a_fg3.eps", replace
